package hello.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class HelloServlet
 */
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ServletConfig config;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public HelloServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// response.getWriter().append("Served at:
		// ").append(request.getContextPath());
		// OutputStream out = response.getOutputStream();
		PrintWriter out = response.getWriter();

		if (this.config != null) {
			// out.write(this.config.getInitParameter("name").getBytes("UTF-8"));
			out.println(this.config.getInitParameter("name"));
		}
		else {
			// out.write("Init parameter is null.".getBytes("UTF-8"));
			out.println("Init parameter is null.");
		}

		if (this.getServletContext() != null) {
			// out.write(this.getServletContext().getInitParameter("name").getBytes("UTF-8"));
			out.println(this.getServletContext().getInitParameter("name"));
		}
		else {
			// out.write("Context parameter is null.".getBytes("UTF-8"));
			out.println("Context parameter is null.");
		}

		Cookie[] cookies = request.getCookies();

		if (cookies == null) {
			out.println("this is the first time you accessed.");
		}
		else {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("lastTime")) {
					out.println("lastAccessTime:" + cookie.getValue());
					break;
				}
			}
		}

		Cookie cookie = new Cookie("lastTime", String.valueOf(System.currentTimeMillis()));
		response.addCookie(cookie);

		HttpSession session = request.getSession();
		session.setAttribute("data", "呵呵");

		String id = session.getId();

		if (session.isNew()) {
			out.println("New session, id is " + id);
		}
		else {
			out.println("Old session, id is " + id);
		}

		String url = request.getContextPath() + "/servlet/hello.html";
		url = response.encodeRedirectURL(url);

		out.println(url);
		response.setHeader("content-type", "text/html;charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		response.setStatus(HttpServletResponse.SC_OK);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		super.init(config);
		this.config = config;
	}

}
